<?php

/**
 * BlogController is the default controller for this application
 */
class BlogController extends BrowseControllerBase {
	/**
	 * AdminBlogDataService object
	 *
	 * @var AdminBlogDataService object
	 */
	public $adminBlogDataService = null;
	
	/**
	 * AdminBlogCommentDataService object
	 *
	 * @var AdminBlogCommentDataService object
	 */
	public $adminBlogCommentDataService = null;
	
	/**
	 * Constructor of index
	 */
	public function __construct() {
		parent::__construct ();
		
		// Set page name
		$this->setPageName ( 'blog' );
		
		// Initialise AdminBlogDataService
		$this->adminBlogDataService = new AdminBlogDataService ();
		
		// Initialise AdminBlogCommentDataService
		$this->adminBlogCommentDataService = new AdminBlogCommentDataService ();
	}
	
	/**
	 * The "index" action is the default action for all controllers -- the
	 * landing page of the site.
	 *
	 * @return void
	 *
	 */
	public function indexAction() {
		//Type maybe next_id or prev_id
		$type = $this->getQuery ( 'type' );
		if (! empty ( $type )) {
			$keywords = explode ( '_', $type );
			
			$id = "";
			$order = "";
			if (count ( $keywords ) == 2) {
				$id = $keywords [1];
				$order = $keywords [0];
			}
			
			if ($order == "next") {
				$itemsList = $this->adminBlogDataService->getNextEntry ( $id, 5 );
			
			} else if ($order == "prev") {
				$itemsList = $this->adminBlogDataService->getPreviousEntry ( $id, 5 );
			}
		} else {
			// Get 5 latest Blog entry
			$arrElement = array ();
			$arrElement ['status'] = AdminBlogEntity::ACTIVE;
			$itemsList = $this->adminBlogDataService->getItems ( $arrElement, " id DESC", 0, 5 );
		
		}
		
		$hasNextEntries = false;
		$hasPreviousEntries = false;
		
		if (is_array ( $itemsList ) && count ( $itemsList ) > 0) {
			$start_id = end ( $itemsList )->id;
			$end_id = $itemsList [0]->id;
			$this->smarty->assign ( 'end_id', $end_id );
			$this->smarty->assign ( 'start_id', $start_id );
			
			$nextItemsList = $this->adminBlogDataService->getNextEntry ( $end_id, 5 );
			
			$prevItemsList = $this->adminBlogDataService->getPreviousEntry ( $start_id, 5 );
			if (is_array ( $itemsList ) && count ( $nextItemsList ) > 0) {
				$hasNextEntries = true;
			}
			
			if (is_array ( $itemsList ) && count ( $prevItemsList ) > 0) {
				$hasPreviousEntries = true;
			}
		
		}
		$this->smarty->assign ( 'blogItemList', $itemsList );
		/*		
		if(count($itemsList) == 1)
		{
			$arrTemp = array();
			$arrTemp[] = $itemsList;
			$this->smarty->assign('blogItemList', $arrTemp);
		}
		*/
		$this->smarty->assign ( 'hasPreviousEntries', $hasPreviousEntries );
		$this->smarty->assign ( 'hasNextEntries', $hasNextEntries );
		
		$this->smarty->assign ( 'numberBlogItemList', count ( $itemsList ) );
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Browse/Blog/index.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Browse/MainLayout.tpl' );
	}
	
	public function detailsAction() {
		$id = $this->getQuery ( 'id' );
		if ($id == '') {
			$id = $this->getPost ( 'id', '' );
		}
		$id = intval ( $id );
		// Get data
		$adminBlogEntity = $this->adminBlogDataService->getById ( $id );
		if (empty ( $adminBlogEntity )) {
			// Go on to login page
			$url = Zend_Registry::get ( 'config' )->APPLICATION_URL;
			CommonUtils::redirect ( $url );
			return - 1;
		}
		
		$this->smarty->assign ( 'blogEntity', $adminBlogEntity );
		
		$actionForm = $this->getPost ( 'actionForm', '' );
		//echo $actionForm;
		if (! empty ( $actionForm ) && $actionForm == 'submit') {
			$author = $this->getPost ( 'author', '' );
			$email = $this->getPost ( 'email', '' );
			$website = $this->getPost ( 'url', '' );
			$comment = $this->getPost ( 'comment', '' );
			
			$adminBlogCommentEntity = $this->adminBlogCommentDataService->beforeCreate ();
			$adminBlogCommentEntity->name = $author;
			$adminBlogCommentEntity->blogId = $id;
			$adminBlogCommentEntity->email = $email;
			$adminBlogCommentEntity->website = $website;
			$adminBlogCommentEntity->content = $comment;
			$adminBlogCommentEntity->commentId = 0;
			$adminBlogCommentEntity->status = AdminBlogCommentEntity::ACTIVE;
			$this->adminBlogCommentDataService->create ( $adminBlogCommentEntity );
			
			//Update CommentCount for Blog Entry
			$adminBlogEntity->commentCount = $adminBlogEntity->commentCount + 1;
			$this->adminBlogDataService->update ( $adminBlogEntity );
			
			$message = 'Your request has just sent successfully';
		}
		
		// Get All Comments of that Blog Entry
		$listComments = $this->adminBlogCommentDataService->getAllQuestions ( $id );
		
		// Get Next and Previous Blog Entry
		$nextEntry = $this->adminBlogDataService->getNextEntry ( $id );
		$prevEntry = $this->adminBlogDataService->getPreviousEntry ( $id );
		
		$this->smarty->assign ( 'listComments', $listComments );
		$this->smarty->assign ( 'id', $id );
		$this->smarty->assign ( 'adminBlogCommentDataService', $this->adminBlogCommentDataService );
		$this->smarty->assign ( 'nextEntryId', ! empty ( $nextEntry ) ? $nextEntry->id : "" );
		$this->smarty->assign ( 'previousEntryId', ! empty ( $prevEntry ) ? $prevEntry->id : "" );
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Browse/Blog/details.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Browse/MainLayout.tpl' );
	}
}
